package com.youxue.controller;

import com.alibaba.fastjson.JSONObject;
import com.cangshi.permission.entity.UserSubject;
import com.youxue.entity.*;
import com.youxue.permission.saver.SubjectHolder;
import com.youxue.service.ShoppingCollectionSeivice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 15c on 2018/1/20.
 */
@Controller
@RequestMapping("/sc")
public class ShoppingCollectionController {
    @Autowired
    ShoppingCollectionSeivice shoppingCollectionSeivice;

    /**
     * 添加收藏或者加入购物车，其中scType=0表示收藏夹；scType=1表示购物车
     *
     * @param shoppingCollection
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    String addSC(@RequestBody(required = false) ShoppingCollection shoppingCollection) {
        ErrorJsonObject errorJsonObject = null;
        CorrectJsonObject correctJsonObject = null;
        if (null == shoppingCollection) {
            errorJsonObject = new ErrorJsonObject(101, "shoppingCollection对象为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        if (null == shoppingCollection.getScClassId() || null == shoppingCollection.getScType()) {
            errorJsonObject = new ErrorJsonObject(102, "shoppingCollection对象属性不完整");
            return JSONObject.toJSONString(errorJsonObject);
        }

        // 获取当前访问对象
        UserSubject subject = (UserSubject) SubjectHolder.getSubject();

        //获取当前的登录信息
        UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
        //获取当前的用户登录id
        int userId = userEntity.getUserId();
        ShoppingCollection reslutShoppingCollection = new ShoppingCollection();
        reslutShoppingCollection = shoppingCollectionSeivice.showAcladdBooleanAdd(userId, shoppingCollection.getScType(), shoppingCollection.getScClassId());
        if (null != reslutShoppingCollection) {
            errorJsonObject = new ErrorJsonObject(104, "该课程已经添加，请勿重复添加");
            return JSONObject.toJSONString(errorJsonObject);
        } else {

            shoppingCollection.setScUserId(userId);
            int result = shoppingCollectionSeivice.addShoppingOrCollection(shoppingCollection);
            if (1 == result) {
                correctJsonObject = new CorrectJsonObject("加入购物车或收藏成功");
                return JSONObject.toJSONString(correctJsonObject);
            } else {
                errorJsonObject = new ErrorJsonObject(103, "添加购物车或者收藏失败，请重试");
                return JSONObject.toJSONString(errorJsonObject);
            }
        }
    }

    /**
     * 获取当前登录用户所有收藏或购物车
     *
     * @param scType 类型（scType=0表示收藏夹；scType=1表示购物车）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/show", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showSCs(@RequestParam(required = false) Integer scType) {
        ErrorJsonObject errorJsonObject = null;
        CorrectJsonObject correctJsonObject = null;
        if (null == scType) {
            errorJsonObject = new ErrorJsonObject(104, "scType为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        // 获取当前访问对象
        UserSubject subject = (UserSubject) SubjectHolder.getSubject();

        //获取当前的登录信息
        UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
        //获取当前的用户登录id
        int userId = userEntity.getUserId();

        List<ShoppingCollection> shoppingCollectionList = shoppingCollectionSeivice.showAllSC(userId, scType);
        if (0 == shoppingCollectionList.size()) {
            errorJsonObject = new ErrorJsonObject(105, "暂未收藏或添加任何课程至购物车");
            return JSONObject.toJSONString(errorJsonObject);
        }
        correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(shoppingCollectionList);
        return JSONObject.toJSONString(correctJsonObject);
    }

    /**
     * 实现一条或多条购物车、收藏夹课程删除
     *
     * @param scIds  多id字符拼接，以‘,’相隔
     * @param scType 类型（scType=0表示收藏夹；scType=1表示购物车）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String deleteSCs(@RequestParam(required = false) String scIds, @RequestParam(required = false) Integer scType) {
        ErrorJsonObject errorJsonObject = null;
        CorrectJsonObject correctJsonObject = null;
        if (null == scIds || null == scType) {
            errorJsonObject = new ErrorJsonObject(106, "参数不完整");
            return JSONObject.toJSONString(errorJsonObject);
        }
        if ("".equals(scIds)) {
            errorJsonObject = new ErrorJsonObject(107, "scIds为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        // 获取当前访问对象
        UserSubject subject = (UserSubject) SubjectHolder.getSubject();

        //获取当前的登录信息
        UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
        //获取当前的用户登录id
        int userId = userEntity.getUserId();
        //以‘,’分割scIds
        String[] scId;
        scId = scIds.split(",");
        int intId;
        int result;
        int allResult = 0;
        String s = "";
        for (String id : scId) {
            intId = Integer.parseInt(id);//[String]待转换的字符串
            result = shoppingCollectionSeivice.deleteShoppingOrCollection(intId, scType, userId);
            allResult += result;
        }
        if (allResult == scId.length) {
            correctJsonObject = new CorrectJsonObject("删除成功");
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            errorJsonObject = new ErrorJsonObject(108, (scId.length - allResult) + "个删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 查询总的收藏了多少课程；也可以前端在获取用户所有收藏时进行返回的数据集合长度提取获得
     * @param scType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/count", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String deleteSCs(@RequestParam(required = false) Integer scType) {
        ErrorJsonObject errorJsonObject = null;
        CorrectJsonObject correctJsonObject = null;
        if ( null == scType) {
            errorJsonObject = new ErrorJsonObject(109, "参数不完整");
            return JSONObject.toJSONString(errorJsonObject);
        }
        // 获取当前访问对象
        UserSubject subject = (UserSubject) SubjectHolder.getSubject();

        //获取当前的登录信息
        UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
        //获取当前的用户登录id
        int userId = userEntity.getUserId();
        int result=shoppingCollectionSeivice.showAuserAllCountAboutShoppingOrCollection(userId,scType);
        correctJsonObject = new CorrectJsonObject("总数查询成功");
        correctJsonObject.setData(result);
        return JSONObject.toJSONString(correctJsonObject);
    }
}
